package com.salton123.whiteboard.ui

import android.graphics.BitmapFactory
import android.view.View
import com.hjq.toast.ToastUtils
import com.lxj.xpopup.XPopup
import com.salton123.ui.base.BaseFragment
import com.salton123.whiteboard.R
import com.salton123.whiteboard.callback.IBoardCallback
import com.salton123.whiteboard.model.bean.BoardType
import com.salton123.whiteboard.model.bean.WhiteBoardData
import com.salton123.whiteboard.widget.BoardView.Companion.mCurrentData
import com.salton123.whiteboard.widget.StrokePopup
import kotlinx.android.synthetic.main.fragment_white_board.*

/**
 * User: wujinsheng1@yy.com
 * Date: 2020/5/22 16:40
 * ModifyTime: 16:40
 * Description:
 */
class WhiteBoardFragment : BaseFragment(), IBoardCallback {
    private var mWhiteBoardDataList = mutableListOf<WhiteBoardData>()
    override fun getLayout(): Int = R.layout.fragment_white_board
    private val strokePopup by lazy { StrokePopup(activity()) }
    override fun initViewAndData() {
        var whiteBoardData = WhiteBoardData()
        mWhiteBoardDataList.add(whiteBoardData)
        mBoardView.updateData(whiteBoardData)
        strokePopup.setCallback(mBoardView)
        mBoardView.setBoardAction(this)
    }

    //显示预览浮窗
    private fun showSketchView() {
        ToastUtils.show("显示预览浮窗")
    }

    override fun initListener() {
        super.initListener()
        setListener(
                R.id.btnAdd,
                R.id.btnStroke,
                R.id.btnEraser,
                R.id.btnUndo,
                R.id.btnRedo,
                R.id.btnPhoto,
                R.id.btnBackground,
                R.id.btnSelect,
                R.id.btnSave,
                R.id.btnClear
        )
    }

    override fun onClick(v: View?) {
        super.onClick(v)
        when (v?.id) {
            R.id.btnAdd -> {
                mBoardView.createThumbnail()
                showSketchView()
            }
            R.id.btnStroke -> {
                viewEnable(btnStroke)
                viewDisable(btnEraser, btnSelect)
                context?.let {
                    XPopup.Builder(it)
                            .atView(v)
                            .asCustom(strokePopup)
                            .show()
                }
                mBoardView.onStrokeType(strokePopup.strokeType)
                mBoardView.onBoardType(BoardType.BOARD_TYPE_STROKE)
            }
            R.id.btnEraser -> {
                viewEnable(btnEraser)
                viewDisable(btnStroke, btnSelect)
                mBoardView.onBoardType(BoardType.BOARD_TYPE_ERASER)
            }
            R.id.btnUndo -> {
                mBoardView.undo()
            }
            R.id.btnRedo -> {
                mBoardView.redo()
            }
            R.id.btnPhoto -> {
            }
            R.id.btnBackground -> {
                mCurrentData.backgroundBitmap = BitmapFactory.decodeResource(resources, R.drawable.doodle_shader5)
                mBoardView.notifyRender()
            }
            R.id.btnSelect -> {
                viewEnable(btnSelect)
                viewDisable(btnStroke, btnEraser)
                mBoardView.onBoardType(BoardType.BOARD_TYPE_SELECTED)
            }
            R.id.btnSave -> {
            }
            R.id.btnClear -> {
                mBoardView.clearAll()
            }
        }
    }

    override fun onAction(type: Int, data: WhiteBoardData) {
        if (type == IBoardCallback.ACTION_UP) {
            if (data.strokeRecordList.isNotEmpty()) {
                viewEnable(btnUndo)
            } else {
                viewDisable(btnUndo)
            }
            if (data.strokeRedoList.isNotEmpty()) {
                viewEnable(btnRedo)
            } else {
                viewDisable(btnRedo)
            }
        }
    }

    private fun viewEnable(vararg view: View) {
        view.forEach { it.alpha = 1.0f }
    }

    private fun viewDisable(vararg view: View) {
        view.forEach { it.alpha = 0.4f }
    }
}